﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace Turners.DMS.WebParts
{
    public partial class SearchCAWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb homeWeb = currentSite.AllWebs["Search"])
                {
                    string searchResultsPageUrl = homeWeb.Url + "/resultsca.aspx?k=";

                    if (SearchAllTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += SearchAllTextBox.Text;
                    }

                    if (DateStartTextBox.Text == String.Empty || DateEndTextBox.Text == String.Empty)
                    {
                        if (DateStartTextBox.Text != String.Empty)
                        {
                            string reformattedDate = DateStartTextBox.Text.Split('/')[1] + "/" + DateStartTextBox.Text.Split('/')[0] + "/" + DateStartTextBox.Text.Split('/')[2];
                            searchResultsPageUrl += " DocumentDate>\"" + reformattedDate + "\"";
                        }
                        if (DateEndTextBox.Text != String.Empty)
                        {
                            string reformattedDate = DateEndTextBox.Text.Split('/')[1] + "/" + DateEndTextBox.Text.Split('/')[0] + "/" + DateEndTextBox.Text.Split('/')[2];
                            searchResultsPageUrl += " DocumentDate<\"" + reformattedDate + "\"";
                        }
                    }else
                    {
                        string reformattedstartDate = DateStartTextBox.Text.Split('/')[1] + "/" + DateStartTextBox.Text.Split('/')[0] + "/" + DateStartTextBox.Text.Split('/')[2];
                        string reformattedendDate = DateEndTextBox.Text.Split('/')[1] + "/" + DateEndTextBox.Text.Split('/')[0] + "/" + DateEndTextBox.Text.Split('/')[2];

                        searchResultsPageUrl += " (DocumentDate>\"" + reformattedstartDate + "\" AND DocumentDate<\"" + reformattedendDate + "\")";  
                    }


                   
                    if (CustomerNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerNumber:\"" + CustomerNumberTextBox.Text + "\"";
                    }
                    if (CustomerNameTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerName:\"" + CustomerNameTextBox.Text + "\"";
                    }

                    searchResultsPageUrl += " DocumentType:\"Credit Application\"&r=fileextension%3D%22pdf%22";

                    Response.Redirect(searchResultsPageUrl);
                }
            }

        }
    }
}
